
package com.shop.cloud.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.mall.common.entity.UserInfo;
import com.shop.cloud.mall.admin.mapper.UserInfoMapper;
import com.shop.cloud.mall.admin.service.UserInfoService;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import java.io.Serializable;

/**
 * 商城用户
 *
 * @author
 * @date 2019-12-04 11:09:55
 */
@Service
@AllArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

	@Override
	@CacheEvict(value = CacheConstants.APP_USER_INFO_CACHE, key = "#entity.getId()")
	@Klock(name = "UserInfo", keys = {"#entity.getId()"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
	public boolean updateById(UserInfo entity) {
		if("".equals(entity.getParentId())){//删除上级
			return super.update(new UpdateWrapper<UserInfo>().lambda()
					.set(UserInfo::getParentId,null)
					.eq(UserInfo::getId,entity.getId())
			);
		}else{
			return super.updateById(entity);
		}
	}

	@Override
	@CacheEvict(value = CacheConstants.APP_USER_INFO_CACHE, key = "#id")
	@Klock(name = "UserInfo", keys = {"#id"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
	public boolean removeById(Serializable id) {
		return super.removeById(id);
	}

	@Override
	public IPage<UserInfo> page1(IPage<UserInfo> page, UserInfo userInfo) {
		return baseMapper.selectPage1(page,userInfo);
	}

}
